package com.zx.jhpt.datamart;

import com.zx.framework.core.pagination.Pagination;
import com.zx.framework.core.util.DatetimeUtils;
import com.zx.framework.core.util.SQLBuilder;
import com.zx.framework.core.util.StringUtil;
import com.zx.framework.db.IBaseDAO;
import com.zx.framework.db.exception.DatabaseException;
import com.zx.framework.plat.user.vo.UserVo;
import com.zx.jhpt.po.Replymessage;
import com.zx.jhpt.service.EngineService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/7.
 */
@Component("dataMartService")
@Transactional
public class DataMartService {
    private static final Logger LOGGER = Logger.getLogger(DataMartService.class);
    @Resource
    private IBaseDAO baseDAO;
    public Pagination<DataMart> page(DataMart dataMart, UserVo queryUser, Integer page, Integer rows) {
        try {
            String qrySql = "select * from "+dataMart.getTableName()+" t where t.operatorId = ?";
            String cntSql = "select count(*) from "+dataMart.getTableName()+" t where 1=1 and t.operatorId = ?";

            List<Object> params = new ArrayList<>();
            List<String> clauses = new ArrayList<>();
            params.add(queryUser.getId());
            //条件查询可以放在这里
            if (!clauses.isEmpty()) {
                String where = SQLBuilder.appendSQL(clauses);
                qrySql += where;
                cntSql += where;
            }
            return baseDAO.sqlPageQuery(qrySql, cntSql, page, rows, params.toArray());
        } catch (Exception e) {
            LOGGER.error("查询日志核查列表失败！", e);
            throw new DatabaseException("查询日志核查列表失败！");
        }
    }

    /**
     * 数据核查操作
     * @param zxid
     * @param tbName
     */
    public String hc(String zxid,String tbName,UserVo loginUser){
        try {
            String sql ="select ZX_STATUS,ACCEPT_OPERATOR from "+tbName +" where ZX_ID = ?";
            List<Map<String, String>> zxStatusList = baseDAO.querySQL(sql,zxid);
            String hcStatus = zxStatusList.get(0).get("ZX_STATUS");
            String operatorId = zxStatusList.get(0).get("ACCEPT_OPERATOR");
            if (!loginUser.getId().equals(operatorId)){
                return "您没有权限操作词条记录.";
            }
            //(1-已核查,0-未核查),
            if ("1".equals(hcStatus)){
                return  "该条记录已核查";
            }else{
                sql = "update "+tbName+" set ZX_STATUS = '1' , ZX_CHECK_TIME = '"+DatetimeUtils.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss")+"' where ZX_ID = ?";
                baseDAO.executeSQL(sql,zxid);
                return  "核查处理成功";
            }
        }catch (IndexOutOfBoundsException e1){
            LOGGER.info("zxid:"+zxid+" in table:"+tbName+" 无记录！");
            throw new DatabaseException("无记录！");
        }catch (Exception e){
            LOGGER.error("查询核查状态失败!");
            throw new DatabaseException("查询日志核查列表失败！");
        }
    }
}
